class TouchMoveDrag extends egret.Sprite {
    private ball: Ball
    private diffX:number = 0
    private diffY:number = 0
    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this)
    }

    private onAddToStage(): void {
        this.ball = new Ball()
        this.ball.x = 100
        this.ball.y = 100
        this.addChild(this.ball)
        this.ball.touchEnabled = true

        this.ball.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchStart, this)
    }

    private onTouchStart(evt: egret.TouchEvent) {
        this.diffX = this.ball.x - evt.stageX
        this.diffY = this.ball.y - evt.stageY
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this)
        this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this)
    }

    private onTouchEnd(evt: egret.TouchEvent) {
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this)
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this)
    }

    private onTouchMove(evt: egret.TouchEvent) {
        this.ball.x = evt.stageX + this.diffX
        this.ball.y = evt.stageY + this.diffY
    }
}